package com.myeclipse.search;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.search.ui.ISearchPageContainer;
import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.ISearchResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SearchQuery implements ISearchQuery{

	private SearchResult searchResult= new SearchResult(this);
	private String searchString;
	private ISearchPageContainer container;
	private int total;

	
	public SearchQuery(String searchString, ISearchPageContainer container){
		this.searchString=searchString;
		this.container=container;
	}
	
	@Override
	public IStatus run(IProgressMonitor monitor)
			throws OperationCanceledException {
		// TODO Auto-generated method stub
		
//		Add the search code to the SearchResult
		monitor.beginTask("code search",100);
		String searchStringFormat=urlFormat(searchString);
		String pageURL="http://searchco.de/api/codesearch_I/?q="+searchStringFormat;
		String result=getHTML(pageURL);
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(result);
			System.out.println(jsonObject.toString());
			JSONArray array=jsonObject.getJSONArray("results");
			for(int i=0;i<array.length();i++)
			{
				String node=array.getJSONObject(i).toString();
				searchResult.add(new Code(node));
				monitor.worked(10);
			}
			this.total=jsonObject.getInt("total");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(monitor!=null)
		{
			monitor.done();
		}
		return Status.OK_STATUS;
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return searchString;
	}

	@Override
	public boolean canRerun() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean canRunInBackground() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public ISearchResult getSearchResult() {
		// TODO Auto-generated method stub
		return searchResult;
	}

	private String getHTML(String pageURL) {
		// TODO Auto-generated method stub
		/*get a String through the URL*/
		StringBuilder pageHTML=new StringBuilder();
		try{
			URL url=new URL(pageURL);
			HttpURLConnection connection=(HttpURLConnection)url.openConnection();
			BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
			String line =null;
			while((line=br.readLine())!=null){
				pageHTML.append(line);
				pageHTML.append("\r\n");
			}
			connection.disconnect();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return pageHTML.toString();
	}
	public int getTotal(){
		return total;
	}

	private String urlFormat(String source){
		/*change the string format according to the URL coding*/
		String result="";
		for(int i=0;i<source.length();i++){
			char letter=source.charAt(i);
			switch(letter){
			case ' ':
				result=result+"%20";
				break;
			case '!':
				result=result+"%21";
				break;
//			case '"':
//				result=result+"%22";
//				break;
			case '#':
				result=result+"%23";
				break;
			case '$':
				result=result+"%24";
				break;
			case '%':
				result=result+"%25";
				break;
			case '&':
				result=result+"%26";
				break;
			case '\'':
				result=result+"%27";
				break;
			case '(':
				result=result+"%28";
				break;
			case ')':
				result=result+"%29";
				break;
//			case '*':
//				result=result+"%2A";
//				break;
			case '+':
				result=result+"%2B";
				break;
			case ',':
				result=result+"%2C";
				break;
//			case '-':
//				result=result+"%2D";
//				break;
//			case '.':
//				result=result+"%2E";
//				break;
			case '/':
				result=result+"%2F";
				break;
			case ':':
				result=result+"%3A";
				break;
			case ';':
				result=result+"%3B";
				break;
//			case '<':
//				result=result+"%3C";
//				break;
			case '=':
				result=result+"%3D";
				break;
//			case '>':
//				result=result+"%3E";
//				break;
			case '?':
				result=result+"%3F";
				break;
			case '@':
				result=result+"%40";
				break;
			case '[':
				result=result+"%5B";
				break;
			case '\\':
				result=result+"%5C";
				break;
			case ']':
				result=result+"%5D";
				break;
			case '^':
				result=result+"%5E";
				break;
//			case '_':
//				result=result+"%5F";
//				break;
			case '`':
				result=result+"%60";
				break;
			case '{':
				result=result+"%7B";
				break;
			case '|':
				result=result+"%7C";
				break;
			case '}':
				result=result+"%7D";
				break;
//			case '~':
//				result=result+"%7E";
//				break;
			default:
				result=result+letter;
				break;
			}
			
		}
		return result;
	}
}
